package com.ziyun.erp.web.modules.logistics.controller;

import com.ziyun.erp.common.e.logistics.LogisticsTypeEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.logistics.entity.LogisticsTypeEntity;
import com.ziyun.erp.modules.logistics.service.LogisticsTypeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 物流类型表
 *
 * @author guizhao
 * @email ...
 * @date 2018-08-14 08:42:25
 */
@RestController
@RequestMapping("logistics/logisticstype")
public class LogisticsTypeController {
    @Autowired
    private LogisticsTypeService logisticsTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("logistics:logisticstype:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = logisticsTypeService.queryPage(params);

        R rs = R.ok();
        rs.put("page", page);

        //存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("logisticsTypeEnum", JsonUtils.enumToList(LogisticsTypeEnum.class));

        rs.put("userData", mapEnum);

        return rs;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{logisticsId}")
    @RequiresPermissions("logistics:logisticstype:info")
    public R info(@PathVariable("logisticsId") Integer logisticsId){
        LogisticsTypeEntity logisticsType = logisticsTypeService.selectById(logisticsId);

        return R.ok().put("logisticsType", logisticsType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("logistics:logisticstype:save")
    public R save(@RequestBody LogisticsTypeEntity logisticsType){

        logisticsType.setCreateName(""+UserUtils.getCurentUser().getUserId());
        logisticsTypeService.insert(logisticsType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("logistics:logisticstype:update")
    public R update(@RequestBody LogisticsTypeEntity logisticsType){
        ValidatorUtils.validateEntity(logisticsType);
        logisticsTypeService.updateAllColumnById(logisticsType);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("logistics:logisticstype:delete")
    public R delete(@RequestBody Integer logisticsId){
        logisticsTypeService.deleteUpdate(logisticsId,UserUtils.getCurentUser().getUserId());

        return R.ok();
    }

}
